package com.ld.caodan.client.impl;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;

import com.ld.caodan.client.IClient;


/**
 *
 *
 * @author 刘笛
 * 2011-9-19  下午04:09:33
 */
public class CCHttpClient implements IClient{
    
    private HttpClient client ;
    
    public CCHttpClient(){
	this.client = new DefaultHttpClient();
    }

    private InputStream getInputStreamByRequest(HttpUriRequest request)
	    throws IOException, ClientProtocolException {
	HttpResponse httpResponse = client.execute(request);
	return httpResponse.getEntity().getContent();
    }
    
    /**
     * post请求
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public InputStream doPost(String url) throws ClientProtocolException,
	    IOException {
	return getInputStreamByRequest(new HttpPost(url));
    }

    public InputStream doGet(String url) throws ClientProtocolException,
	    IOException {
	return getInputStreamByRequest(new HttpGet(url));
    }

    /**
     * delete请求
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public InputStream doDelete(String url) throws ClientProtocolException,
	    IOException {
	return getInputStreamByRequest(new HttpDelete(url));
    }

    /**
     * put请求
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public InputStream doPut(String url) throws ClientProtocolException,
	    IOException {
	return getInputStreamByRequest(new HttpPut(url));
    }

    /**
     * head请求
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public InputStream doHead(String url) throws ClientProtocolException,
	    IOException {
	return getInputStreamByRequest(new HttpHead(url));
    }

    /**
     * option请求
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public InputStream doOptions(String url) throws ClientProtocolException,
	    IOException {
	return getInputStreamByRequest(new HttpOptions(url));
    }
}
